package lancooPaperContent;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.PaperDef;

import org.apache.commons.lang3.StringEscapeUtils;

import static testPaperCTCS.GuessTestingPaperActivity.InforGuessTesting;
import static testPaperFavoritesPage.FavoritesPagePaperActivity.InforFavoritesPage;
import static testPaperJBFX.BasicReviewPaperActivity.InforBasicReview;
import static testPaperNDFX.DifficultReviewPaperActivity.InforDifficultReview;
import static testPaperWrong.WrongQuesPaperActivity.InforWrongQues;
import static testPaperWrongHistory.HistoryWrongQuesPaperActivity.InforHistoryWrongQues;

/**
 * Created by Administrator on 2017/9/27/027.
 */

public class ContentChppAdapter extends WrapBaseAdapter

{

    private LayoutInflater mInflater;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源


    private ViewHolder holder = null;
    public class ViewHolder{
        WebView WvTheme;
        public ViewHolder(View convertView) {
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
        }
    }

    //    private int totalItemCount;//小题数量
    private String theme;//获取大题题干信息1


    public ContentChppAdapter(Context context, int sumItem, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.ModelFrom =modelFrom;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_content_chpp_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
        * 获取试题相关信息
        * */
        getInfor();

        /*
        * 显示大题题干
        * */
        getContent();

        return convertView;
    }

    /*
    * 获取试题信息
    * */
    private void getInfor(){

        if(ModelFrom == PaperDef.FROM_BASE_REVIEW) {
            theme =InforBasicReview.getPaperContent().get(PaperID).getTheme();
//            String arrayTheme[] = new String[totalItemCount+1];
//            String themeAll = "";
//            for(int i = 0;i<totalItemCount+1;i++) {
//                arrayTheme[i] = Control.returnArray(theme, "____", 4)[i];
//                if(i<totalItemCount) {
//                    themeAll = themeAll + arrayTheme[i] + "<font color='#3399cc'><middle><middle><U>"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"（"+(i+1)+"）"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
//                }else{
//                    themeAll = themeAll + arrayTheme[i];
//                }
//            }
//            theme = StringEscapeUtils.unescapeXml(themeAll);

        }else if(ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW){
            theme = InforDifficultReview.getTheme();
        }else if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){
            theme = InforGuessTesting.getPaperContent().get(PaperID).getTheme();
        }else if(ModelFrom== PaperDef.FROM_BASE_REVIEW_WRONG||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG){
            theme =InforWrongQues.get(PaperID).getTheme();

        }else if(ModelFrom==PaperDef.FROM_BASE_REVIEW_WRONG_History||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG_History){
            theme = InforHistoryWrongQues.get(PaperID).getTheme();
        }else if(ModelFrom==PaperDef.FROM_FAVOURITE_REVIEW){
            theme = InforFavoritesPage.getTheme();
        }

    }


    /*
    * 获取题目题干信息
    * */
//    Handler htmlHandlerTheme = new Handler();
    private void getContent(){

//        new Thread(new Runnable()
//        {
//            @Override
//            public void run()
//            {
//                theme = StringEscapeUtils.unescapeXml(theme);
//                final Spanned textTheme = Html.fromHtml(theme, Control.imgGetter,null);
//                htmlHandlerTheme.post(new Runnable()
//                {
//                    @Override
//                    public void run()
//                    {
//                        holder.TvTheme.setText(textTheme);
//                    }
//                });
//
//            }
//        }).start();
        theme = StringEscapeUtils.unescapeXml(theme);
        holder.WvTheme.loadDataWithBaseURL("about:blank",theme,"text/html","utf-8",null);

    }

}